﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class KhachHangDAO
    {
        ConnectDB conn = new ConnectDB();
        string str;
        //Load dữ liệu
        public DataTable Load_KhachHang()
        {
            return conn.LayDuLieu("select *from KhachHang");
        }
        //Insert va Update
        public void IsUp_KhachHang(KhachHangDTO p, bool flag)
        {
            string MaKH = p.MaKH;
            string TenKH = p.TenKH;
            string DC = p.DiaChi;
            string DT = p.DienThoai;
            bool loaikh = p.LoaiKH;
            float diemtl = p.DiemTichLuy;
            if (flag == true)
            {//(MaHD, MaSP, MaKH, MaKH, SoLuong, TongTien, NgayMua, MaNV, GiaBan)
                str = "INSERT KhachHang (TenKH, DiaChi, SoDT, LoaiKH, DiemTichLuy) VALUES(N'" + TenKH + "',N'" + DC + "','" + DT + "','" + loaikh + "'," + diemtl + ")";
            }
            else
            {
                str = "UPDATE KhachHang SET TenKH =N'" + TenKH + "', DiaChi =N'" + DC + "', SoDT ='" + DT + "', LoaiKH='" + loaikh + "', DiemTichLuy=" + diemtl + " WHERE MaKH='" + MaKH + "'";
            }
            conn.excute(str);
        }
        //Del Khach hang
        public void Del_KhachHang(KhachHangDTO p)
        {
            string MaKH = p.MaKH ;
            str = "DELETE KhachHang WHERE MaKH='" + MaKH + "'";
            conn.excute(str);
        }

        public DataTable TimKH(KhachHangDTO  p, bool flag)
        {
            string MaKH = p.MaKH ;
            string TenKH = p.TenKH ;
            if (flag == true)
            {
                str = "select * from KhachHang where MaKH='" + MaKH + "'";
            }
            else
            {
                str = "select * from KhachHang where TenKH like'%" + TenKH  + "%'";
            }
            return conn.LayDuLieu(str);

        }
    }
}
